package book.enums;

import java.util.EnumSet;
import java.util.Map;
import java.util.TreeMap;

/**
 * 图书状态枚举:
 */
public enum BookStatus {
    DELETE(0,"删除"),
    NORMAL(1,"可借阅"),
    FORBIDDEN(2,"不可借阅"),
    ;

    private Integer code;//数字信息
    private String desc;//描述信息

    BookStatus(Integer code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    /**
     * 根据数字返回 描述信息 (以枚举的方式)
     * @param code
     * @return 返回枚举对象
     * 法一:
     */
    public static BookStatus getPageStatusByCode(Integer code){
        switch(code){
            case 0: return DELETE;
            case 1: return NORMAL;
            case 2:
            default:return FORBIDDEN;
        }
    }
//    法二: 返回描述信息
    public static String getPageStatusByCode2(Integer code){
        switch(code){
            case 0: return DELETE.getDesc();
            case 1: return NORMAL.getDesc();
            case 2:
            default:return FORBIDDEN.getDesc();
        }
    }
//    法三: 通过静态代码块,将枚举信息放到map中,通过get方法获取到value
    public static Map<Integer,String> map=new TreeMap<>() ;
    static{
        for(BookStatus status: EnumSet.allOf(BookStatus.class)){
            map.put(status.getCode(),status.getDesc());
        }
    }
    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }
}
